class_name Camera
extends Camera2D

const DISTANCNE_TARGET := 100.0
const SMOOTHING_SPEED_CARRIED := 2
const SMOOTHING_SPEED_DEFAULT := 8
const DURATION_SHAKING := 120
const SHAKE_INTENSITY := 5

@export var ball: Ball

var is_shaking := false
var time_start_sharking := 0

func _init() -> void:
	GameEvent.impact_received_request.connect(on_impact_received.bind())

func _process(_delta: float) -> void:
	if ball.carried != null:
		position = ball.carried.position + ball.carried.heading * DISTANCNE_TARGET
		position_smoothing_speed = SMOOTHING_SPEED_CARRIED
	else:
		position = ball.position
		position_smoothing_speed = SMOOTHING_SPEED_DEFAULT
	if is_shaking and Time.get_ticks_msec() - time_start_sharking < DURATION_SHAKING:
		offset = Vector2(randf_range(-SHAKE_INTENSITY, SHAKE_INTENSITY), randf_range(-SHAKE_INTENSITY, SHAKE_INTENSITY))
	else:
		is_shaking = false
		offset = Vector2.ZERO
		
func on_impact_received(_impact_position: Vector2, is_high_impact: bool) -> void:
	if is_high_impact:
		is_shaking = true
		time_start_sharking = Time.get_ticks_msec()
